package com.allinpay.merchantweb.controller;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;


import com.allinpay.merchantweb.common.exception.ViewException;
import com.allinpay.merchantweb.common.utils.MD5;
import com.allinpay.merchantweb.common.utils.SequenceUtil;
import com.allinpay.merchantweb.common.utils.StringUtil;
import com.allinpay.merchantweb.constants.CommonConstants;
import com.allinpay.merchantweb.model.Manager;
import com.allinpay.merchantweb.model.UserLoginInfo;
import com.allinpay.merchantweb.request.CommonReqBean;
import com.allinpay.merchantweb.request.ManagerRequest;
import com.allinpay.merchantweb.response.AppCommonResponse;
import com.allinpay.merchantweb.response.ResponseMessage;
import com.allinpay.merchantweb.service.ICommPrivilegeService;
import com.allinpay.merchantweb.service.LoginService;


@Controller
public class LoginController {
	private static final Logger logger = LoggerFactory.getLogger(LoginController.class);
	 
    @Autowired
    private LoginService loginServiceImpl;
    
    @Autowired
	ICommPrivilegeService commPrivilegeService;
    /**
     * 功能：默认登录页
     *
     */
    @RequestMapping(value = "")
    public String loginInit(HttpServletRequest request) {
        return "system/login";
    }
    
    /**
     * 功能：登陆操作
     *
     */
    @RequestMapping(value = "/login")
    public String login(@ModelAttribute Manager manager, Model model, HttpServletRequest request) {
        //1.获取到登陆参数
    	String userAccount=manager.getUserAccount();
    	String orgCode=manager.getOrgCode();
    	String passWord=manager.getPassWord();
    	//2.判断非空
    	if(StringUtil.isEmpty(userAccount) || StringUtil.isEmpty(orgCode) || StringUtil.isEmpty(passWord)) {
    		model.addAttribute("loginerror", "企业代码，用户名和密码不能为空");
    		return "system/login";
    	}
    	//3.调用服务层
    	passWord=MD5.MD5Encode(passWord);
        CommonReqBean<ManagerRequest> requestMessage = new CommonReqBean<ManagerRequest>();
        requestMessage.setReqSsn(SequenceUtil.getAppReqSsn());
        ManagerRequest bizcontent=new ManagerRequest();
        bizcontent.setOrgCode(orgCode);
        bizcontent.setUserAccount(userAccount);
        bizcontent.setPassWord(passWord);
        requestMessage.setBizContent(bizcontent);
        try {
        	AppCommonResponse<Manager> repMsg = loginServiceImpl.login(requestMessage);
			//获取返回的数据
        	Manager sessionManage=repMsg.getData();
			//计入Session
			request.getSession().setAttribute(CommonConstants.SESSIONMANAGER, sessionManage);
			//获取登录用户的权限列表
			Object menuObject = commPrivilegeService.getEntryMenuTreeByAccount(sessionManage.getUserAccount());
			model.addAttribute("navs", menuObject);
		       
		} catch (ViewException e) {
			e.printStackTrace();
			model.addAttribute("loginerror", "该用户无权限访问系统！");
			return "system/login";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			model.addAttribute("loginerror", e.getMessage());
		}
         
    	return "system/index";
    }
    
    /**
     * 功能：主页操作
     *
     */
    @RequestMapping(value = "/main")
    public String main(@ModelAttribute UserLoginInfo userLoginInfo, Model model, HttpServletRequest request) {
        return "system/main";
    }
}
